<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    :bodyStyle="bodyStyle"
    style="top:15%;height: 70%;"
  >
    <template slot="footer">
      <a-button key="back" v-if="isReadOnly" @click="handleCancel">
        关闭
      </a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" id="customerModal">
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
            <a-input disabled placeholder="请输入客户名称" v-decorator.trim="['customerName', validatorRules.name]" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="内部编码">
            <a-input disabled placeholder="请输入所属行业" v-decorator.trim="['contractCode']" />
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="业务负责人">
            <a-select
              :disabled="show"
              v-decorator.trim="['applicant', validatorRules.name]"
              allowClear
              placeholder="请选择业务负责人"
              showSearch
              optionFilterProp="children"
            >
              <a-select-option v-for="(item, index) in personList" :key="index" :value="item">
                {{ item }}
              </a-select-option>
            </a-select>
          </a-form-item>
        </a-col>
        <a-col :span="24 / 2">
          <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="发货说明">
             <a-textarea placeholder="请输入发货说明" v-decorator.trim="['shippingRemark']" :rows="4" />
          </a-form-item>
        </a-col>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
import pick from 'lodash.pick'
import { addApproval, personList, putApproval } from '@/api/api'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { autoJumpNextInput } from '@/utils/util'
export default {
  name: 'ShipmentApproval',
  mixins: [JeecgListMixin],
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      personList: [],
      show: null,
      FList: [
        { id: 1, name: '否' },
        { id: 0, name: '是' }
      ],
      statusList: [
        {
          id: 1,
          name: '审批通过'
        },
        {
          id: 0,
          name: '待审批'
        },
        {
          id: 2,
          name: '驳回'
        }
      ],
      bodyStyle: {
        padding: '20',
        height: window.innerHeight * 0.7 + 'px',
        'overflow-y': 'auto'
      },
      isReadOnly: false,
      state: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      defaultFileList: null,
      form: this.$form.createForm(this),
      url: {
        importExcelUrl: '/systemConfig/upload' + '?' + `biz=fh`
      },
      validatorRules: {
        name: {
          rules: [{ required: true, message: '请输入!' }]
        }
      }
    }
  },
  created() {},
  mounted() {
    personList().then(res => {
      if (res.code == 200 || res.code == 10000) {
        this.personList = res.data
      } else {
        this.$message.warning(res.message)
      }
    })
  },
  methods: {
    //选中为不符合
    handleChange(value) {
      console.log(value)
      if (value == '1') {
        this.state = true
      } else {
        this.state = false
      }
    },
    add(record) {
      this.edit(record)
      console.log(record)
    },
    edit(record) {
      let isShow = JSON.parse(sessionStorage.getItem('UserMessage'))
      this.show = isShow
      this.form.resetFields()
      this.model = Object.assign({}, record)
      this.visible = true
      console.log(this.model)
      this.$nextTick(() => {
        let aaa = JSON.parse(sessionStorage.getItem('UserMessage'))
        if (aaa) {
          let a = JSON.parse(sessionStorage.getItem('User'))
          this.form.setFieldsValue({ applicant: a.ename })
        }
        this.form.setFieldsValue(
          pick(
            this.model,
            'shippingDate',
            'shippingRemark',
            'approver',
            'auditor',
            'isSendable',
            'reviewer',
            'notSendableDesc',
            'customerName',
            'contractCode',
            'applicant',
            'contractNum'
          )
        )
        autoJumpNextInput('customerModal')
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let formData = Object.assign(values)
          if (this.model.beginNeedGet && this.model.beginNeedPay) {
            that.$message.warn('期初应收和期初应付不能同时输入')
            that.confirmLoading = false
            return
          }
          console.log(this.model)
          if (this.model.earlyWarningDate) {
            formData.contractId = this.model.id
          } else {
            formData.id = this.model.id
          }
          // formData.shippingDate = this.$moment(formData.shippingDate).format('YYYY-MM-DD')
          let obj
          if (this.model.earlyWarningDate) {
            formData.status = 0
            obj = addApproval(formData)
          } else {
            obj = putApproval(formData)
          }
          obj
            .then(res => {
              if (res.code === 200 || res.code === 10000) {
                this.$message.success('保存成功')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    validateSupplierName(rule, value, callback) {
      let params = {
        name: value,
        type: '客户',
        id: this.model.id ? this.model.id : 0
      }
      checkSupplier(params).then(res => {
        if (res && res.code === 200) {
          if (!res.data.status) {
            callback()
          } else {
            callback('名称已经存在')
          }
        } else {
          callback(res.data)
        }
      })
    }
  }
}
</script>
<style scoped></style>
